﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class KhoThietBiDAO
    {
        SqlConnection conn = null;

        public List<KhoThietBiDTO> LayDanhSachKhoThietBi()
        {
            List<KhoThietBiDTO> lisKTB = new List<KhoThietBiDTO>();
            SqlDataReader reader = null;
            try
            {
                conn = DataProvider.ConnectDB();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_LayDanhSachKhoThietBi";
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    KhoThietBiDTO ktbDTO = new KhoThietBiDTO();
                    ktbDTO.MaKho = reader.GetInt32(0);
                    ktbDTO.MaThietBi = reader.GetInt32(1);
                    ktbDTO.SoLuongTon = reader.GetInt32(2);

                    lisKTB.Add(ktbDTO);
                }
            }
            catch (Exception ex)
            { String e = ex.ToString(); }
            finally
            { conn.Close(); }
            return lisKTB;
        }

        public string ThemKhoThietBi(KhoThietBiDTO ktbDTO)
        {
            string msg = "";
            try
            {
                conn = DataProvider.ConnectDB();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_ThemKhoThietBi";
                SqlParameter para;
                para = cmd.Parameters.Add("@MaThietBi", SqlDbType.Int);
                para.Value = ktbDTO.MaThietBi;
                para = cmd.Parameters.Add("@SoLuongTon", SqlDbType.Int);
                para.Value = ktbDTO.SoLuongTon;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (SqlException ex)
            {
                msg = ex.Message;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            return msg;
        }

        public string CapNhatKhoThietBi(KhoThietBiDTO ktbDTO)
        {
            string msg = "";
            try
            {
                conn = DataProvider.ConnectDB();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_CapNhatKhoThietBi";
                SqlParameter para;
                para = cmd.Parameters.Add("@MaKho", SqlDbType.Int);
                para.Value = ktbDTO.MaKho;
                para = cmd.Parameters.Add("@MaThietBi", SqlDbType.Int);
                para.Value = ktbDTO.MaThietBi;
                para = cmd.Parameters.Add("@SoLuongTon", SqlDbType.Int);
                para.Value = ktbDTO.SoLuongTon;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (SqlException ex)
            {
                msg = ex.Message;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            return msg;
        }

        public string XoaKhoThietBi(KhoThietBiDTO ktbDTO)
        {
            string msg = "";
            try
            {
                conn = DataProvider.ConnectDB();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "usp_XoaKhoThietBi";
                SqlParameter para;
                para = cmd.Parameters.Add("@MaKho", SqlDbType.Int);
                para.Value = ktbDTO.MaKho;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (SqlException ex)
            {
                msg = ex.Message;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            return msg;
        }
    }
}
